package cn.szzsi.business.controller.admin;

import cn.szzsi.AConfig;
import cn.szzsi.business.service.CompanyAuthLogService;
import cn.szzsi.business.service.CompanyInfoService;
import cn.szzsi.common.email.SendEmail;
import cn.szzsi.common.util.CommonUtil;
import cn.szzsi.model.CompanyAuthLog;
import cn.szzsi.model.CompanyInfo;
import cn.szzsi.model.PlateformConfig;
import com.jfinal.aop.Before;
import com.jfinal.aop.Clear;
import com.jfinal.kit.HttpKit;
import com.jfinal.kit.PropKit;
import com.jfinal.kit.Ret;
import com.jfinal.kit.StrKit;
import com.jfinal.plugin.activerecord.tx.Tx;
import com.jfinal.plugin.ehcache.CacheKit;
import com.jfinal.plugin.redis.Redis;
import org.json.JSONObject;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by admin on 2017/6/26.
 */
public class CompanyInfoController extends BaseAdminController<CompanyInfo>{
    private String shopId;

    public void index(){
        String shopId=getAttr("shopId");
        CompanyInfo ci=CompanyInfo.dao.getByShopId(shopId);
        Long registerTime=ci.getCreateDate().getTime()/1000;
        String status=ci.getStatus();//审核状态:0-适用,1-审核中,2-审核通过,3-审核不通过
        Long countDown=(registerTime+(5*24*60*60))-(new Date().getTime()/1000);
        if("0".equals(status) && countDown<=0){//试用期,并且倒计时小于或者等于0,跳转到认证界面
            redirect("/shop/companyinfo/certificationForm");
        }else if("1".equals(status) && countDown<=0){//审核中,并且倒计时小于或者等于0,跳转到审核中界面
            redirect("/shop/companyinfo/incertificationForm");
        }else if("3".equals(status) && countDown<=0){//审核不通过,并且倒计时小于或者等于0,跳转到认证界面
            redirect("/shop/companyinfo/failverified");
        }

    }
    public void detail(){
        shopId=getAttr("shopId");
        CompanyInfo ci=CompanyInfo.dao.getByShopId(shopId);
        setAttr("ci",ci);
        String categories=ci.getCategories();
        if(StrKit.notBlank(categories)){
            String categoriesArr[]=categories.split(",");
            setAttr("category_one",categoriesArr[0]);
            setAttr("category_two",categoriesArr[1]);
        }

        String status=ci.getStatus();
        if("0".equals(status)){//试用期
            render(CommonUtil.TEMPLATE_PATH + "/business/companyinfo/certification_menu.html");
        }else if("1".equals(status)){//审核中
            render(CommonUtil.TEMPLATE_PATH + "/business/companyinfo/incertification_menu.html");
        }else if("2".equals(status)){//审核通过
            render(CommonUtil.TEMPLATE_PATH + "/business/companyinfo/success_certification.html");
        }else if("3".equals(status)){//审核不通过
            System.out.println(ci.getId());
            CompanyAuthLog cal=CompanyAuthLog.dao.getByCompanyInfoId(ci.getId());
            setAttr("cal",cal);
            render(CommonUtil.TEMPLATE_PATH + "/business/companyinfo/fail_certification_menu.html");
        }
    }

    /**
     * 审核信息不通过,通过菜单点击修改企业信息弹出表单
     */
    public void updateCompanyInfoForm(){
        shopId=getAttr("shopId");
        CompanyInfo ci=CompanyInfo.dao.getByShopId(shopId);
        setAttr("ci",ci);
        String categories=ci.getCategories();
        String categoriesArr[]=categories.split(",");
        setAttr("category_one",categoriesArr[0]);
        setAttr("category_two",categoriesArr[1]);
        render(CommonUtil.TEMPLATE_PATH + "/business/companyinfo/certification_menu.html");
    }


    /**
     * 正在审核页面
     */
    public void incertificationForm(){
        render(CommonUtil.TEMPLATE_PATH + "/business/companyinfo/incertification.html");
    }

    /**
     * 实名认证(不通过主菜单点击)
     */
    public void certificationForm(){
        shopId=getAttr("shopId");
        CompanyInfo ci=CompanyInfo.dao.getByShopId(shopId);
        setAttr("ci",ci);
        String categories=ci.getCategories();
        String categoriesArr[]=categories.split(",");
        setAttr("category_one",categoriesArr[0]);
        setAttr("category_two",categoriesArr[1]);
        render(CommonUtil.TEMPLATE_PATH + "/business/companyinfo/certification.html");
    }

    /**
     * 实名认证(通过菜单点击)
     */
    public void menuCertificationForm(){
        String id=getPara("id");
        CompanyInfo ci=CompanyInfo.dao.findById(id);
        setAttr("ci",ci);
        render(CommonUtil.TEMPLATE_PATH + "/business/companyinfo/certification_menu.html");
    }

    /**
     * 审核不通过
     */
    public void failverified(){
        shopId=getAttr("shopId");
        CompanyInfo ci=CompanyInfo.dao.getByShopId(shopId);
        CompanyAuthLog cal=CompanyAuthLog.dao.getByCompanyInfoId(ci.getId());
        setAttr("ci",ci);
        setAttr("cal",cal);
        render(CommonUtil.TEMPLATE_PATH + "/business/companyinfo/fail_certification.html");
    }

    /**
     * 更新企业认证信息(实名认证保存)
     */
    @Before(Tx.class)
    public void certificationSave(){
        shopId=getAttr("shopId");
        String categories=getPara("categories");//所属行业
        String brandLogo=getPara("brandLogo");
        String brandName=getPara("brandName");
        String company_name=getPara("company_name");
        String business_licence_code=getPara("business_licence_code");
        String business_licence_img=getPara("business_licence_img");
        String imgName=getPara("imgName");
        String email=getPara("email");
        String emailCode=getPara("emailCode");
        String personInCharge=getPara("personInCharge");
        String phone=getPara("phone");
        String phoneCode=getPara("phoneCode");
        String cacheEmailCode=CacheKit.get("emailCode",email);
        if(StrKit.isBlank(cacheEmailCode)){
            renderJson(Ret.fail("info","邮箱验证码错误"));
            return;
        }
        if(!emailCode.equals(cacheEmailCode.toString())){
            renderJson(Ret.fail("info","邮箱验证码错误"));
            return;
        }
        if(CacheKit.get("sms",phone)==null){
            renderJson(Ret.fail("info","手机验证码错误"));
            return;
        }
        if(!phoneCode.equals(CacheKit.get("sms",phone).toString())){
            renderJson(Ret.fail("info","手机验证码错误"));
            return;
        }
        CompanyInfo co=CompanyInfo.dao.getByShopId(shopId);
        CompanyAuthLogService.service.saveInfo(co.getId(),"提交","","2","商户提交认证信息");//保存审核日志
        CompanyInfoService.cis.updateInfo(co,company_name,business_licence_code,business_licence_img,email,personInCharge,phone,"1",brandName,brandLogo,categories,imgName);//更新认证信息
        renderJson(Ret.ok("info","提交成功"));

    }

    /**
     * 更改是否推送系统消息
     */
    public void updateTipsInfo(){
        shopId=getAttr("shopId");
        CompanyInfo co=CompanyInfo.dao.getByShopId(shopId);
        String tipsInfo=getPara("tipsInfo");
        co.setTipsInfo(tipsInfo);
        co.update();
        renderJson(Ret.ok());

    }


    /**
     * 获取邮箱验证码
     */
    @Clear
    public void getEmailCode(){
        String email=getPara("email");
        String emailCode=CommonUtil.getSixRandom();
        String text="欢迎您使用微米荟商城。您的验证码为:"+emailCode+",请在30分钟内使用";
        String result=SendEmail.send(email,text,"微米荟商城邮箱验证");
        if("success".equals(result)){
            CacheKit.put("emailCode",email,emailCode);
            renderJson(Ret.ok("info","发送成功"));
        }else{
            renderJson(Ret.fail("info","发送成功"));

        }
    }

    /**
     * 获取手机验证码
     */
    @Clear
    public void getPhoneCode(){
        String phone=getPara("phone");
        String captcha = CommonUtil.getSixRandom();//六位纯数字验证码
        PlateformConfig plateformConfig=PlateformConfig.dao.findById(1);
        String sms_signature=plateformConfig.getSmsSignature();
        String message = "【"+sms_signature+"】验证码"+captcha+" ，请勿将验证码告知他人。";
        Map map=new HashMap();
        map.put("shopId","0");
        map.put("phone",phone);
        map.put("content",message);
        map.put("sms_type","yzm");
        map.put("app_key","zshop");
        map.put("captcha",captcha);
        String reqUrl= AConfig.p.get("sms")+"/sms/send";//短信服务器接口;
        String result= HttpKit.get(reqUrl,map);
        JSONObject obj=new JSONObject(result);
        if("0".equals(obj.get("code").toString())){
            renderJson(Ret.ok("info","发送成功"));
            CacheKit.put("sms",phone,captcha);
            System.out.println("captcha-------------"+captcha);
        }else{
            renderJson(Ret.fail("info",obj.get("desc")));
        }
    }


}
